package org.skyui.views;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;
import org.skyui.beans.NewsBean;
import org.skyui.core.UIApplication;

import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

public class NewsView extends Panel implements UIView {

	@Override
	public String getExplanation() {
		// TODO Auto-generated method stub
		return "";
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "News";
	}

	@Override
	public void setExplanation(String explanation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub

	}

	public NewsView() {
		this.setScrollable(true);
		this.setHeight("800px");
		refresh();
	}

	public void refresh() {
		VerticalLayout vl = (VerticalLayout) this.getLayout();
		vl.removeAllComponents();
		Panel newsPanel = new Panel();

		BeanSetManager mgr = BeanSetManager.getManager(NewsBean.class,
				UIApplication.getInstance().getOrganization());
		List refreshedBeans = mgr.getRefreshedBeansAsList();

		Collections.sort(refreshedBeans, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				return ((AbstractBean) o2).getCreationTime().compareTo(
						((AbstractBean) o1).getCreationTime());
			}
		});

		for (Object bean : refreshedBeans) {
			Label l = new Label(bean.toString());
			l.setContentMode(Label.CONTENT_XHTML);
			newsPanel.addComponent(l);
			Label l1 = new Label();
			l1.setHeight("10px");
			newsPanel.addComponent(l1);
		}
		vl.addComponent(newsPanel);

	}

}
